package com.kun.kunkunmall.cart.controller;

import com.kun.kunkunmall.cart.service.CartService;
import com.kun.kunkunmall.cart.vo.Cart;
import com.kun.kunkunmall.cart.vo.CartItem;
import com.kun.kunkunmall.cart.vo.UserInfoVo;
import com.kun.kunkunmall.cart.interceptor.CartInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 获取登录用户购物车里面所有被选中的购物项
     * @return
     */
    @GetMapping("/currentUserCartItem")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItem() {
        return cartService.getCurrentUserCartItem();
    }


    /**
     * 删除购物项
     *
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.kunkunmall.com/cart.html";
    }

    /**
     * 改变商品的数量
     *
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.changeItemCount(skuId, num);
        return "redirect:http://cart.kunkunmall.com/cart.html";
    }


    /**
     * 勾选每一个购物项
     *
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("check") Integer check) {
        cartService.checkItem(skuId, check);
        return "redirect:http://cart.kunkunmall.com/cart.html";
    }


    /**
     * 跳转到购物车页面
     * 浏览器有一个叫做user-key的cookie，一个月后过期
     *
     * @return
     */
    @GetMapping("/cart.html")
    public String cartList(Model model) throws ExecutionException, InterruptedException {
//        UserInfoVo userInfoVo = CartInterceptor.threadLocal.get();
//        System.out.println(userInfoVo);
        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     *
     * @return 重定向到成功页面
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        CartItem item = cartService.addToCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.kunkunmall.com/addToCartSuccess.html";
    }


    /**
     * 跳转到购物车添加成功页面
     *
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        // 重定向到成功页面，再次查询购物车数据即可
        CartItem item = cartService.getCartItem(skuId);
        model.addAttribute("item", item);
        return "success";
    }

}
